package com.cyneck.demo.service;

import com.cyneck.demo.constant.ColorEnumFactory;
import com.cyneck.demo.utils.ImageHelper;
import com.cyneck.demo.utils.ImageUtils;
import com.cyneck.demo.utils.MergeImageUtil;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>Description: 图片处理服务</p>
 *
 * @author Eric Lee
 * @version v1.0.0
 * @since 2020/9/18 10:29
 **/

@Component
public class MergeImageService {


    /**
     * 活动照片模板合成
     *
     * @param targetImageFile 输入照片
     * @return 合成照片
     * @throws Exception
     */
    public File TemplateMergeImage(File targetImageFile) throws Exception {

        //模板位置
        String fileName = System.getProperty("user.dir") + File.separator + "image" + File.separator + "template-p.png";
        File scrFile = new File(fileName);
        BufferedImage templateImage = ImageIO.read(scrFile);

        BufferedImage targetBufferImage = ImageIO.read(targetImageFile);

        //缩放裁剪图片
        BufferedImage resizeBufferImage = ImageHelper.resize(targetBufferImage, 410, 410);
//        BufferedImage targetImage = ImageIO.read(compressFile);
        //加工合成图
        BufferedImage bufferedImage = MergeImageUtil.mergePng(templateImage, resizeBufferImage, 295, 207, 1.0f);

        File saveFile = File.createTempFile("temp", "_composite.jpg");
        // 生成图片
        String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
        ImageIO.write(bufferedImage, formatName, saveFile);
        return saveFile;
    }


    public String changeColor(File targetFile, String color) throws Exception {
        BufferedImage image = ImageIO.read(targetFile);
        int w = image.getWidth();
        int h = image.getHeight();

        //创建一个width xheight ，RGB高彩图，类型可自定
        BufferedImage background = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        //取得图形
        Graphics graphics = background.getGraphics();
        //设置颜色
        graphics.setColor(ColorEnumFactory.create(color));
        //填充
        graphics.fillRect(0, 0, w, h);
        //加工合成图
        BufferedImage bufferedImage = MergeImageUtil.mergePendant(background, image, 0, 0, 1.0f);
        InputStream inputStream = ImageUtils.bufferedImageToInputStream(bufferedImage);
        String imageBase64Str = ImageUtils.getImageBase64Str(inputStream);

        return imageBase64Str;
    }


    /**
     * 拼接复制图片到6寸（1200px * 1600px）相纸
     *
     * @param targetFile
     * @return
     * @throws Exception
     */
    public String splicingImage(File targetFile) throws Exception {
        BufferedImage image = ImageIO.read(targetFile);
        int w = image.getWidth();
        int h = image.getHeight();

        int interval = 30;

        int number = (1200 / (w + interval)) < (1600 / (h + interval)) ? (1200 / (w + interval)) : (1600 / (h + interval));
        //计算复制张数合成后的图片的w和h
        int innerW = number * (w + interval) + interval;
        int innerH = number * (h + interval) + interval;

        //创建一个width xheight ，RGB高彩图，类型可自定
        BufferedImage background = new BufferedImage(innerW, innerH, BufferedImage.TYPE_INT_RGB);
        //取得图形
        Graphics graphics = background.getGraphics();
        //设置颜色
        graphics.setColor(ColorEnumFactory.WHITE.create());
        //填充
        graphics.fillRect(0, 0, innerW, innerH);

        //加工合成图
        int hOffset = 0;
        for (int i = 0; i <= number; i++) {
            hOffset = interval + (h + interval) * i;
            int wOffset = 0;
            for (int j = 0; j <= number; j++) {
                wOffset = interval + (w + interval) * j;
                background = MergeImageUtil.mergePendant(background, image, wOffset, hOffset, 1.0f);
            }
        }


        //创建临时图片
        BufferedImage tmpBufferedImage = new BufferedImage(1200, 1600, BufferedImage.TYPE_INT_RGB);
        //取得图形
        Graphics graph = tmpBufferedImage.getGraphics();
        //设置颜色
        graph.setColor(ColorEnumFactory.WHITE.create());
        //填充
        graph.fillRect(0, 0, 1200, 1600);

        //合成居中
        background = MergeImageUtil.mergePendant(tmpBufferedImage, background, (1200 - innerW) / 2, (1600 - innerH) / 2, 1.0f);

        InputStream inputStream = ImageUtils.bufferedImageToInputStream(background);
        String imageBase64Str = ImageUtils.getImageBase64Str(inputStream);
        return imageBase64Str;
    }

    public static void main(String[] args) throws Exception {
        MergeImageService service = new MergeImageService();
        service.splicingImage(new File("D:\\data\\pic\\pic6-1Inch.jpg"));
//        System.out.println(file.getAbsolutePath());

    }

}
